package com.yanfan.maintain.quartz;




import com.yanfan.common.constant.Constants;
import com.yanfan.maintain.domain.InspectionPlan;
import com.yanfan.maintain.domain.InspectionTask;
import com.yanfan.maintain.mapper.InspectionPlanDao;
import com.yanfan.maintain.service.impl.InspectionServiceImpl;
import com.yanfan.quartz.domain.SysJobLog;
import com.yanfan.quartz.service.ISysJobLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;


@Component("taskJob")
public class TaskJob  {

    @Autowired
    private InspectionServiceImpl inspectionService;

    @Autowired
    InspectionPlanDao inspectionPlanDao;


    @Autowired
    ISysJobLogService iSysJobLogService;


    public void taskInit() {
//        long Id= Long.parseLong("(SELECT MAX(id) FROM inspection_plan)".toString());
        try {

//           巡检规则（1、时，2、周，3、日）
            InspectionPlan inspectionPlan = inspectionPlanDao.queryOderById();
//            inspectionPlanDao.queryOderById();
//            InspectionPlan inspectionPlan = new InspectionPlan();
            if(inspectionPlan == null){
                return;
            }
//            SysJobLog sysJobLog = new SysJobLog();
//            sysJobLog.setJobName(inspectionPlan.getName() + inspectionPlan.getNumber());
//            sysJobLog.setJobGroup("定时执行巡检任务");
//            sysJobLog.setInvokeTarget("taskJob.taskInit");
//            sysJobLog.setRemark(inspectionPlan.getId().toString());
//            List<SysJobLog> sysJobLogs = iSysJobLogService.selectJobLogList(sysJobLog);
           //TODO
            inspectionService.addInspectionPlan(inspectionPlan);
//            insertJobLog(inspectionPlan);
            Date date = inspectionPlan.getCreatedDate();
            LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();

            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DAY_OF_MONTH, -1);
            //前24小时
            Date HourAgo = calendar.getTime();
            if(HourAgo.getTime() == (new Date()).getTime()) {
                System.out.println("这是前一天的提醒！时间：" + HourAgo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void insertJobLog(InspectionPlan inspectionPlan) {
        SysJobLog sysJobLog = new SysJobLog();
        sysJobLog.setJobName(inspectionPlan.getName() + inspectionPlan.getNumber());
        sysJobLog.setJobGroup("定时执行巡检任务");
        sysJobLog.setInvokeTarget("taskJob.taskInit");
        sysJobLog.setRemark(inspectionPlan.getId().toString());
        sysJobLog.setStartTime(new Date());
        sysJobLog.setStopTime(new Date());
        sysJobLog.setStatus(Constants.SUCCESS);
        sysJobLog.setJobMessage(inspectionPlan.getExecutor().toString());
        iSysJobLogService.addJobLog(sysJobLog);
    }

}
